package com.zzty.zzlp.serviceimpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.zzty.zzlp.dao.*;
import com.zzty.zzlp.entity.SelfExam;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.SelfExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SelfExamServiceImpl implements SelfExamService {
    @Autowired
    SelfExamMapper sed;

    @Autowired
    SelfExamTransationMapper setd;

    @Autowired
    UserDao ud;

    @Autowired
    QuestionMapper qd;

    public boolean judgeQstExist(int id) {

        Date date = new Date();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
        System.out.println("user:" + ud.selectUserById(id).toString());
        Integer clazzid = ud.selectUserById(id).getClazzId();
        try {
            if (sed.qstExist(clazzid, dateStr) == null) {

                return false;
            }
            if (sed.qstExist(clazzid, dateStr).getQuestions() != null) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public ArrayList searchQstId(int cName) {
        ArrayList<Integer> List = new ArrayList<Integer>();
        String[] strarr;
        Date date = new Date();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
        try {
            String qid = sed.searchQstId(dateStr, cName);
            strarr = qid.split("\\|");
            for (int i = 0; i < strarr.length; i++) {
                List.add((Integer.parseInt(strarr[i])));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return List;
    }

    public ArrayList searchQstContent(ArrayList arr) {
        int[] iarr = new int[arr.size()];
        for (int i = 0; i < arr.size(); i++) {
            iarr[i] = (Integer) arr.get(i);
        }
        ArrayList<String> questions = new ArrayList<String>();
        try {
            for (int i = 0; i < arr.size(); i++) {

                questions.add(qd.selectQuecontent(iarr[i] + "").getContent() + "\n");
                // questions.add(sed.searchQstContent(iarr[i]) + "\n");
                // questions.add("test" + "\n");
                //
                // sarr[i]
                // =
                // sed.searchQstContent(iarr[i]);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return questions;
    }

    public String searchClazzByUserId(int id) {
        try {
            return (ud.selectUserById(id).getClazzId() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer searchExamId(int userId) {
        Date date = new Date();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
        try {
            return (sed.searchExId(userId, dateStr));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Integer searchIdByDateAndClass(String date, int clazzId) {
        if (sed.searchIdByDateAndClass(date, clazzId) == null) {
            return null;

        }
        return (sed.searchIdByDateAndClass(date, clazzId));
    }

    public ArrayList searchQstId(String date, int cName) {
        ArrayList<Integer> List = new ArrayList<Integer>();
        String[] strarr;
        String qid;
        try {
            qid = sed.searchQstId(date, cName);
            strarr = qid.split("\\|");
            for (int i = 0; i < strarr.length; i++) {
                List.add((Integer.parseInt(strarr[i])));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return List;
    }

    public Integer saveSelfExam(SelfExam se) {
        try {
            return sed.insertSelfExam(se);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void updateWinner(int examId, int winnerId) {
        sed.updateWinner(examId, winnerId);

    }

    @Override
    public List<SelfExam> searchSelfExamByClazzId(Integer clazzId, Integer page) {
        return sed.searchSelfExamByClazzId(clazzId, page);
    }

    @Override
    public List<SelfExam> searchSelfExamByClazzId(Integer clazzId) {
        return sed.searchSelfExamOnlyByClazzId(clazzId);
    }

    @Override
    public Integer searchNewId(String date, String questions, Integer clazzId) {
        return sed.searchNewId(date, questions, clazzId);
    }

    @Override
    public List<SelfExam> findLastFiveTeamWin(Integer clazzId) {

        return sed.findLastFiveTeamWin(clazzId);
    }

    @Override
    public List<SelfExam> searchSelfExamByWinnerIdAndClazzId(Integer clazzId) {
        return sed.searchSelfExamByWinnerIdAndClazzId(clazzId);

    }

    @Override
    public List<SelfExam> searchTenSelfExamByClazzId(Integer clazzId, Integer page) {

        return sed.searchTenSelfExamByClazzId(clazzId, page);
    }
}
